package com.keshe.springboot_keshe.controller;

import com.keshe.springboot_keshe.common.Result;
import com.keshe.springboot_keshe.entity.User;
import com.keshe.springboot_keshe.entity.Zhanghao;
import com.keshe.springboot_keshe.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public Result<Zhanghao> login(@RequestBody Zhanghao zhanghao) {
        System.out.println("Login request received for user: " + zhanghao.getUsername());
        return userService.login(zhanghao);
    }

    @PostMapping("/user/list")
    public Result getUserList(@RequestBody Map<String, Object> params) {
        Integer pageNum = params.get("pageNum") != null ? 
            Integer.parseInt(params.get("pageNum").toString()) : 1;
        Integer pageSize = params.get("pageSize") != null ? 
            Integer.parseInt(params.get("pageSize").toString()) : 10;
        
        params.put("pageNum", pageNum);
        params.put("pageSize", pageSize);
        
        return userService.getUserList(params);
    }

    @PostMapping("/user/add")
    public Result addUser(@RequestBody User user) {
        return userService.addUser(user);
    }

    @PostMapping("/user/update")
    public Result updateUser(@RequestBody User user) {
        return userService.updateUser(user);
    }

    @PostMapping("/user/delete")
    public Result deleteUser(@RequestParam Integer id) {
        return userService.deleteUser(id);
    }

    @PostMapping("/user/delete/batch")
    public Result batchDeleteUser(@RequestBody List<Integer> ids) {
        return userService.batchDeleteUser(ids);
    }
}
